//
//  ClubAddressListViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/20.
//

import UIKit

class ClubAddressListViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("地址管理").color(.black).font(16.font_semiBold).end()
    private let addButton = c_make_ex(UIButton.self)
        .bgColor("#FF831A").color(.white).text("添加新地址")
        .font(16.font_semiBold).corner(22).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubAddressCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 130.0.Scale
        return tableView
    }()
    private var emptyView = ClubAddressEmptyView()
    private var sourceData = [ClubMineAddressModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            backButton
            titleLabel
            tableView
            emptyView
            addButton
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.Leading == backButton.Trailing
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height)
        emptyView.leading(0).trailing(0).top(.navi_height + 210.0.Scale).height(180)
        addButton.leading(24).trailing(24).height(44.0.Scale).bottom(.safe_bottom)
        tableView.Bottom == addButton.Top
        
        emptyView.isHidden = true
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        addButton.addTarget(self, action: #selector(addNewAddress), for: .touchUpInside) 
    }

    private func loadData(){
        
        Task {
            let res = await network.request(.myAddressList,type: ClubMineAddressModel.self,isShowError: false)
            if res.isSuccess, let models = res.models {
                
                self.emptyView.isHidden = models.count > 0
                self.sourceData = models
                self.tableView.reloadData()
            }
        }
    }
    
    private func delAddress(md: ClubMineAddressModel){
        
        Task {
            let res = await network.requestSpecial(.delAddress(addressID: md.id))
            if res.isSuccess {
                self.loadData()
            }
        }
    }
}

//MARK: action
extension ClubAddressListViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return sourceData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubAddressCell", for: indexPath) as! ClubAddressCell
        cell.model = sourceData[indexPath.row]
        cell.delBlock = { md in
            self.delAddress(md: md)
        }
        cell.editBlock = { md in
            let addVC = ClubCreateAddressViewController()
            addVC.isEdit = true
            addVC.model = md
            addVC.updateAddressData = {
                self.loadData()
            }
            self.navigationController?.pushViewController(addVC, animated: true)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let addVC = ClubCreateAddressViewController()
        addVC.isEdit = true
        addVC.model = sourceData[indexPath.row]
        addVC.updateAddressData = {
            self.loadData()
        }
        self.navigationController?.pushViewController(addVC, animated: true)

    }
    
    @objc private func addNewAddress(){
        
        let addVC = ClubCreateAddressViewController()
        addVC.isEdit = false
        addVC.updateAddressData = {
            self.loadData()
        }
        self.navigationController?.pushViewController(addVC, animated: true)
    }
}
